#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>

int dns_parse(const char *domain_name,char *ip)
{
    struct addrinfo *answer, hint, *curr;
    char ipstr[16];
    char (*ips)[16] = ip;
    int num = 0;

    bzero(&hint, sizeof(hint));
    hint.ai_family = AF_INET;
    hint.ai_socktype = SOCK_STREAM;

    int ret = getaddrinfo(domain_name, NULL, &hint, &answer);
    if (ret != 0) {
        fprintf(stderr,"getaddrinfo: %s\n",
                gai_strerror(ret));
        return -1;
    }

    for (curr = answer; curr != NULL; curr = curr->ai_next) {
        inet_ntop(AF_INET,
                  &(((struct sockaddr_in *)(curr->ai_addr))->sin_addr),
                  ipstr, 16);
        memcpy(ips+num,ipstr,strlen(ipstr));
        //printf("%s\n", ips+num);
        num++;
    }
    freeaddrinfo(answer);
    return num;
}

int main(int argc, char **argv)
{
    if (argc != 2) {
        fprintf(stderr, "Usage: %s hostname\n",
                argv[1]);
        exit(1);   
    }
#if 0
    struct addrinfo *answer, hint, *curr;
    char ipstr[16];   
    bzero(&hint, sizeof(hint));
    hint.ai_family = AF_INET;
    hint.ai_socktype = SOCK_STREAM;

    int ret = getaddrinfo(argv[1], NULL, &hint, &answer);
    if (ret != 0) {
        fprintf(stderr,"getaddrinfo: %s\n",
                gai_strerror(ret));
        exit(1);
    }

    for (curr = answer; curr != NULL; curr = curr->ai_next) {
        inet_ntop(AF_INET,
                  &(((struct sockaddr_in *)(curr->ai_addr))->sin_addr),
                  ipstr, 16);
        printf("%s\n", ipstr);
    }

    freeaddrinfo(answer);
#else
    int i;
    char ipss[10][16]={0};
    char (*ips)[16]= &ipss[0][0];
    int num = dns_parse(argv[1],ips);
    for(i=0;i<num;i++){
        printf("%d:%s\n",i,ips+i);
    }
#endif
    exit(0);
}
